package com.matrix.yukun.matrix.main_module.wallpaper.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.matrix.yukun.matrix.BaseFragment;
import com.matrix.yukun.matrix.R;
import com.matrix.yukun.matrix.main_module.entity.WallpaperInfo;
import com.matrix.yukun.matrix.main_module.netutils.NetworkUtils;
import com.matrix.yukun.matrix.main_module.utils.SpacesDoubleDecoration;
import com.matrix.yukun.matrix.main_module.utils.SpacesItemDecoration;
import com.matrix.yukun.matrix.main_module.wallpaper.activity.WallpaperCategoryActivity;
import com.matrix.yukun.matrix.main_module.wallpaper.activity.WallpaperPreviewActivity;
import com.matrix.yukun.matrix.main_module.wallpaper.adapter.RVWallpaperMessageAdapter;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/10/28.
 */

public class WallpaperFragment extends BaseFragment {

    @BindView(R.id.ll_phone_wallpaper)
    LinearLayout llPhoneWallpaper;
    @BindView(R.id.ll_computer_wallpaper)
    LinearLayout llComputerWallpaper;
    @BindView(R.id.tv_new_wallpaper)
    TextView tvNewWallpaper;
    @BindView(R.id.tv_hot_wallpaper)
    TextView tvHotWallpaper;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.sw_refresh)
    SwipeRefreshLayout swRefresh;
    private int skip;
    private String HOT = "hot";
    private String NEW = "new";
    private String url = "http://service.picasso.adesk.com/v1/vertical/vertical?";
    private List<WallpaperInfo> mWallpaperInfos = new ArrayList<>();
    private RVWallpaperMessageAdapter mAdapter;
    private String mCurrenType;

    public static WallpaperFragment getInstance() {
        WallpaperFragment toolFragment = new WallpaperFragment();
        return toolFragment;
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_wallpaper_layout;
    }

    @Override
    public void initView(View inflate, Bundle savedInstanceState) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        recyclerview.setLayoutManager(gridLayoutManager);
        mAdapter = new RVWallpaperMessageAdapter(R.layout.wallpaper_index_layout, mWallpaperInfos);
        recyclerview.setAdapter(mAdapter);
        recyclerview.addItemDecoration(new SpacesItemDecoration(10, 10, 0, 0) {
        });
        mCurrenType = HOT;
        initData(mCurrenType);
        initListener();
    }

    public void initListener() {
        mAdapter.setOnLoadMoreListener(() -> {
            skip = skip + 12;
            initData(mCurrenType);
        }, recyclerview);

        swRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                skip = 0;
                mWallpaperInfos.clear();
                mAdapter.setNewData(mWallpaperInfos);
                initData(mCurrenType);
            }
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> WallpaperPreviewActivity.start(getContext(), mWallpaperInfos.get(position),true));
    }

    private void initAnim() {
        //通过加载XML动画设置文件来创建一个Animation对象；
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.wallpaper_anim);
        //得到一个LayoutAnimationController对象；
        LayoutAnimationController lac = new LayoutAnimationController(animation);
        //设置控件显示的顺序；
        lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
        //设置控件显示间隔时间；
        lac.setDelay(0.3f);
        //为ListView设置LayoutAnimationController属性；
        recyclerview.setLayoutAnimation(lac);
    }

    // limit=12&skip=12&adult=false&first=0&order=hot
    private void initData(String order) {
        NetworkUtils.networkGet(url)
                .addParams("limit", 12 + "")
                .addParams("skip", skip + "")
                .addParams("adult", "false")
                .addParams("first", 12 + "")
                .addParams("order", order).build()
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String msg = jsonObject.optString("msg");
                            if (msg.equals("success")) {
                                JSONObject res = jsonObject.optJSONObject("res");
                                Gson gson = new Gson();
                                List<WallpaperInfo> vertical = gson.fromJson(res.optJSONArray("vertical").toString(), new TypeToken<List<WallpaperInfo>>() {
                                }.getType());
                                if (vertical.size() > 0) {
                                    mWallpaperInfos.addAll(vertical);
                                    mAdapter.setNewData(mWallpaperInfos);
                                    mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
                                }
                                swRefresh.setRefreshing(false);
                                mAdapter.loadMoreComplete();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }
                });

    }

    @OnClick({R.id.ll_phone_wallpaper, R.id.ll_computer_wallpaper, R.id.tv_new_wallpaper, R.id.tv_hot_wallpaper})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_new_wallpaper:
                tvNewWallpaper.setTextColor(getContext().getResources().getColor(R.color.color_ff4081));
                tvHotWallpaper.setTextColor(getContext().getResources().getColor(R.color.color_text));
                skip = 0;
                mCurrenType = HOT;
                mWallpaperInfos.clear();
                mAdapter.setNewData(mWallpaperInfos);
                initData(HOT);
                break;
            case R.id.tv_hot_wallpaper:
                tvHotWallpaper.setTextColor(getContext().getResources().getColor(R.color.color_ff4081));
                tvNewWallpaper.setTextColor(getContext().getResources().getColor(R.color.color_text));
                skip = 0;
                mCurrenType = NEW;
                mWallpaperInfos.clear();
                mAdapter.setNewData(mWallpaperInfos);
                initData(NEW);
                break;
            case R.id.ll_phone_wallpaper:
                WallpaperCategoryActivity.start(WallpaperCategoryActivity.PHONE, getContext());
                break;
            case R.id.ll_computer_wallpaper:
                WallpaperCategoryActivity.start(WallpaperCategoryActivity.COMPUTER, getContext());
                break;
        }
    }

}

